{% extends "dissemin/details.html" %}

{% load staticfiles %}
{% load statuses %}
{% load domain %}
{% load orcid %}
{% load i18n %}
{% load widget_tweaks %}
{% load capture_tags %}

{% block headTitle %}
{{ head_search_description }}
{% endblock %}

{% block extra_head %}
{% include "statistics/headers.html" %}
<script type="text/javascript" src="{% static 'papers/refresh.js' %}"></script>
{{ form.media }}
{% endblock %}

{% block bodyTitle %}
{{ search_description }}
{% endblock %}

{% block jsScript %}
$(function(){
       function claim_unclaim (evt) {
         var obj = $(evt.target);
         var pk = obj.attr("data-pk");
         var action = obj.attr("data-action");
         if (action != "claim" && action != "unclaim") {
           // action currently ongoing
           return;
         }
         var my_url = "";
         if (action == "claim") {
           obj.text("{% trans "Claiming..." %}");
           my_url = "{% url "ajax-claimPaper" %}";
         } else {
           obj.text("{% trans "Unclaiming..." %}");
           my_url = "{% url "ajax-unclaimPaper" %}";
         }
         obj.toggleClass("disabled");
         $.ajax({
           method: "post",
           url: my_url,
           data: {"pk": pk,
             "csrfmiddlewaretoken": '{{csrf_token}}'},
           cache: false,
           success: function (data) {
             if (action == "claim") {
               obj.text("{% trans "Exclude from my profile" %}");
               obj.attr("data-action", "unclaim");
             } else {
               obj.text("{% trans "Include in my profile" %}");
               obj.attr("data-action", "claim");
{% if user_researcher == researcher %}
               // user_researcher is only defined on profile views
               // in which case it is equal to the researcher of the currently
               // logged in user; if it is the profile currently being viewed,
               // we must remove the paper entirely
               $("#paper-" + pk).fadeOut(300, function() { $(this).remove(); });
{% endif %}
             }
             obj.toggleClass("claim-button");
             obj.toggleClass("unclaim-button");
             obj.toggleClass("disabled");
           },
           fail: function(data) {
             if (action == "claim") {
               obj.text("{% trans "Claiming failed!" %}");
             } else {
               obj.text("{% trans "Unclaiming failed!" %}");
             }
             obj.toggleClass("disabled");
           }
           });
       }

       function add_claim_unclaim_handlers () {
         $(".unclaim-button").click(claim_unclaim);
         $(".claim-button").click(claim_unclaim);
       }

       add_claim_unclaim_handlers();

       var config = {
         refreshURL: "{{ ajax_url }}",
         refreshMessage: "{% trans "Refreshing results" %}",
         logoHelpNode: $('.logoHelpPopover'),
         refetchButtonNode: $('#refetchPubsButton'),
         hook: add_claim_unclaim_handlers,
         waitingAreaContainerNode: $('#waitingAreaContainer'),
         paperSearchResultsNode: $('#paperSearchResults'),
         nbPapersFoundNode: $('#nbPapersFound'),
         animatedBirdGIF: "{% static "img/animated-bird.gif" %}",
{% if researcher %}
         refetchURL: "{% url "refetch-researcher" researcher.pk %}",
         refetchMessage: "{% trans "Refreshing profile" %}",
         currentTask: {% if researcher.current_task %}"{{ researcher.get_current_task_display }}"{% else %} null {% endif %},
         affiliationForm: $('#affiliationForm'),
         affiliationFormInput: $('#id_value'),
         setResearcherDepartmentURL: "{% url 'ajax-setResearcherDepartment' %}",
{% endif %}
         isSuperUser: {{ request.user.is_superuser|yesno:"true,false,false" }},
         initialMessages: {{ messages | safe }}
       }
       init_paper_module(config);
    });
{% endblock %}

{% block details %}
{% if researcher %}
<span class="detailsTitle">{% trans "Researcher" %}</span>
    <div class="detailsContent">
        <p id="detail-name">{{ researcher }}</p>
        {% if not request.user.is_authenticated and not researcher.orcid %}
        <p class="centered">
        <a href="{% url 'my-profile' %}" class="btn btn-primary"><img src="{% static "img/orcid-small.png" %}" alt="ORCID" /> {% trans "Is it you?" %}</a>
        </p>
        {% endif %}
        {% if researcher.role %}
        <p><span id="researcher-role"
        data-params="{csrfmiddlewaretoken:'{{csrf_token}}'}"
        data-pk="{{ researcher.pk }}">{{ researcher.role }}</span></p>
        {% endif %}
        {% if researcher.homepage %}
        <p><span class="glyphicon glyphicon-home"></span> <a href="{{ researcher.homepage }}">{{ researcher.homepage|domain }}</a></p>
        {% endif %}
        {% if researcher.orcid %}
        <p><a href="{{ researcher.orcid|orcid_to_url }}"><img src="{% static "img/orcid-small.png" %}" alt="ORCID" /> {{ researcher.orcid }}</a></p>
        {% endif %}
        {% if researcher.institution %}
        <p><span class="glyphicon glyphicon-star"></span> {{ researcher.institution }}</p>
        {% endif %}
        {% if researcher.department %}
        <p><span class="glyphicon glyphicon-star-empty"></span> <a href="{{ researcher.department.url }}">{{ researcher.department }}</a></p>
        {% endif %}
	{% if researcher.user == request.user or request.user.is_superuser %}
	<p><a href="#" id="refetchPubsButton">{% trans "Refresh publications" %}</a></p>
	{% endif %}
        {% if request.user.is_superuser and researcher.email %}
        <p>{% trans "Address:" %} {{ researcher.email }}</p>
        {% endif %}
        {% if request.user.is_superuser %}
        <p>{% trans "Names:" %}
            <ul>
            {% for nv in researcher.name_variants.all %}
            <li><a href="{% url 'search' %}?authors={{ nv.name_id }}">{{ nv.name }}</a> ({{ nv.confidence }})</li>
            {% endfor %}
         </ul></p>
         
         <form method="POST" id="affiliationForm">
         {% csrf_token %}
         {{ researcher.affiliation_form }}</br>
         </form>
         {% endif %}
    </div>
{% elif department %}
<span class="detailsTitle">{% trans "Department" %}</span>
    <div class="detailsContent">
        <p id="detail-name">{{ department }}</p>
    </div>
{% elif journal %}
<span class="detailsTitle">{% trans "Journal" %}</span>
    <div class="detailsContent">
        <p id="detail-name">{{ journal }}</p>
        <p>{% trans "Published by" %} <a href="{{ journal.publisher.canonical_url }}">{{ journal.publisher }}</a></p>
        {% if journal.issn %}
        <p>{% trans "ISSN:" %} {{ journal.issn }}</p>
        {% endif %}
        <div class="unselectedCriterion">
        <div class="pubLogo"><div class="pubLogoPic-{{ journal.publisher.oa_status }}"></div></div>
        <div class="criterionItem">{{ journal.publisher.oa_status |explain_oa_status}}</div>
    </div>

    <p><span class="glyphicon glyphicon-stats"></span>
    {% blocktrans trimmed count nb_publi=journal.stats.num_tot|default:0 %}
    {{ nb_publi }} paper published in {{ journal }}
    {% plural %}
    {{ nb_publi }} papers published in {{ journal }}
    {% endblocktrans %}
    </p>
    </div>
{% elif publisher %}
<span class="detailsTitle">{% trans "Publisher" %}</span>
    <div class="detailsContent">
        <p><a href="{{ publisher.canonical_url }}">{{ publisher }}</a></p>
        <div class="unselectedCriterion">
        <div class="pubLogo"><div class="pubLogoPic-{{ publisher.oa_status }}"></div></div>
        <div class="criterionItem">{{ publisher.oa_status |explain_oa_status}}</div>
        </div>

    <p><span class="glyphicon glyphicon-stats"></span>
    {% blocktrans trimmed count nb_publi=publisher.stats.num_tot|default:0 %}
    {{ nb_publi }} paper published by {{ publisher }}
    {% plural %}
    {{ nb_publi }} papers published by {{ publisher }}
    {% endblocktrans %}
    </p>
    </div>
{% endif %}

<span class="detailsTitle">{% trans "Search" %}</span>
<div class="detailsContent">
<p id="nbPapersFound">{{ nb_results }}</p>

<form method="get" action="." id="searchPapers" onChange="$(this).submit()">
    <div class="form-group" id="searchPaperCombinedStatus">
    {% with combined_status_form=True %}
    {% with combined_status=form.status %}
    {% include "statistics/pie_without_object.html" with stats=search_stats object_id=researcher.object_id stats_counter=1 %}
    {% endwith %}
    {% endwith %}
    <noscript>{# Clarifies the usage of the checkboxes in the caption of the pie. #}
    <div class="form-group">
        <button type="submit" class="btn btn-default" form="searchPapers">{% trans "Search" %}</button>
    </div>
    </noscript>
    </div>
    <div class="form-group" id="searchPapersName">
        <label>{% trans "Search by title" %}</label>
        <div class="input-group">
        {{ form.q.errors }}
        {% render_field form.q class="form-control" %}
        <label class="input-group-btn">
            <button type="submit" class="btn btn-default">{% trans "Search" %}</button>
        </label>
        </div>
    </div>
    <div class="form-group" id="searchAuthorsName">
        <label for="{{ form.authors.id_for_label }}">{% trans "By authors" %}</label>
        {{ form.authors.errors }}
        {% render_field form.authors class="form-control" %}
    </div>
    <div class="form-horizontal">
        <label>{% trans "By publication date" %}</label>
        <div class="form-group" id="searchPaperPubDateAfter">
            {{ form.pub_after.errors }}
            <label class="col-xs-2 col-sm-3 control-label tightLabel" for="{{ form.pub_after.id_for_label }}">{% trans "after" %}</label>
            <div class="col-xs-10 col-sm-9 tightInput">
                {% render_field form.pub_after class="form-control" %}
            </div>
        </div>
        <div class="form-group" id="searchPaperPubDateBefore">
            {{ form.pub_before.errors }}
            <label class="col-xs-2 col-sm-3 control-label tightLabel" for="{{ form.pub_before.id_for_label }}">{% trans "before" %}</label>
            <div class="col-xs-10 col-sm-9 tightInput">
            {% render_field form.pub_before class="form-control" %}
            </div>
        </div>
    </div>
    <div class="form-group" id="searchPapersDoctype">
        <label>{% trans "By document type" %}</label>
        {{ form.doctypes.errors }}
        <ul class="compactList">
            {% for checkbox in form.doctypes %}
            {% with value=checkbox.choice_value %}
            <li class="checkbox">
                <label class="doctype">
                    {{ checkbox.tag }}
                    {{ checkbox.choice_label }}
                </label>
            </li>
            {% endwith %}
            {% endfor %}
        </ul>
    </div>
    <div class="form-group form-inline" id="searchPapersSortBy">
        {{ form.reverse_order.errors }}
        {{ form.sort_by.errors }}
        {% capture as sort_by silent %}
        <label for="{{ form.sort_by.id_for_label }}">{% trans "Sort by" %}</label>
        {% endcapture %}
        {% capture as increasing silent %}
        {% render_field form.reverse_order class="form-control tightForm" %}
        {% endcapture %}
        {% capture as value silent %}
        {% render_field form.sort_by class="form-control tightForm" %}
        {% endcapture %}
        {% blocktrans trimmed %}
        {{ sort_by }} <div>{{ increasing }} {{ value }}</div>
        {% endblocktrans %}
    </div>
    {% if request.user.is_superuser %}
    <div class="form-group" id="searchPapersVisibility">
        <label for="{{ form.visibility.id_for_label }}">{% trans "By visibility" %}</label>
        {{ form.visibility.errors }}
        {% render_field form.visibility class="form-control" %}
    </div>
    <div class="form-group" id="searchPapersPDFAvailability">
        <label for="{{ form.availability.id_for_label }}">{% trans "By full text availability" %}</label>
        {{ form.availability.errors }}
        {% render_field form.availability class="form-control" %}
    </div>
    <div class="form-group" id="searchPapersOAStatus">
        {{ form.oa_status.errors }}
        <label for="{{ form.oa_status.id_for_label }}">{% trans "By publisher policy" %}</label>
        {% for checkbox in form.oa_status %}
        {% with value=checkbox.choice_value %}
        <div class="logoHelpPopover" data-content="{{ value|helptext_oa_status }}">
        <div class="checkbox publisherCriterion">
            {{ checkbox.tag }}
            <div class="publisherCriterionBox">
            <label for="{{ checkbox.id_for_label }}">
                <img alt="" src="{% static value|logo_oa_status %}" height="70px" width="52px" class="pubLogo publisherCriterionLogo">
                <span class="criterionItem">{{ checkbox.choice_label }}</span>
            </label>
            </div>
        </div>
        </div>
        {% endwith %}
        {% endfor %}
    </div>
    {% endif %}
    <div class="form-group">
        <button type="submit" class="btn btn-default">{% trans "Search" %}</button>
    </div>
</form>
</div>
{% endblock %}

{% block lists %}
<div id="waitingAreaContainer">
</div>

 <ul class="messages">
 </ul>

 <div id="paperSearchResults">
     {% include "papers/paperList.html" %}
 </div>
{% endblock %}
